import gzip
import os

import fileUtil

'''
解压该目录所有文件
'''


def unzip_gz_dir(in_dir, out_dir):
    for root, dirs, files in os.walk(in_dir):
        for f in files:
            if f.endswith('.gz'):
                unzip_gz_file(os.path.join(root, f), os.path.join(out_dir, f.replace('.gz', '.csv')))


'''
gz_file_path 文件路径
output_file_path 解压路径
'''


def unzip_gz_file(gz_file_path, output_file_path):
    fileUtil.mkdir(output_file_path)

    try:
        # 打开 .gz 文件
        with gzip.open(gz_file_path, 'rb') as gz_file:
            # 读取 .gz 文件的内容
            content = gz_file.read()
            # 打开输出文件并写入内容
            with open(output_file_path, 'wb') as output_file:
                output_file.write(content)
        print(f"解压成功，文件已保存到 {output_file_path}")
    except Exception as e:
        print(f"解压过程中出现错误: {e}")


if __name__ == '__main__':
    unzip_gz_dir(r'D:\temp\HGUPERIODIC_202503121915',
                 r'D:\temp\HGUPERIODIC_202503121915\file')
